<?php

class Booking_EditController extends Crb_Controller
{
	function preDispatch()
	{
        		parent::preDispatch();
	}
	
	function getDatesAction()
	{
		$this->setNoRender();
		$this->disableLayout();
		
		
		$type = $this->_getParam('type',0);
		
		$bookDAO = new Book();
		$blockDates = $bookDAO->getBlockDates($type);
		
		$dateRange = array();    
		
		foreach ($blockDates as $blockDate)
		{
			$Date = strtotime($blockDate) ;
			$digitDate = array(  date("m", $Date), date("d", $Date),  date("Y", $Date) );	
			$dateRange[] = $digitDate;	
		}
		
		echo  Zend_Json::encode($dateRange); 
	}
	
		
	public function indexAction()
	{
		$this->view->layout()->setLayout('admin');
		
		
		$orderDAO = new Order();
		$this->view->Rows = $orderDAO->getList();
		
		
		if ($this->getRequest()->isPost())
		{
			$dateFrom = $this->_getParam('from');
			$dateTo   = $this->_getParam('till');
			$roomType = $this->_getParam('type');
			$bookDAO = new Book();
			$bookDAO->setBlockInterval($dateFrom, $dateTo, $roomType);
		}
	}
	
	
	public function processAction()
	{
$this->view->layout()->setLayout('admin');		
		
		$id = $this->_getParam('id');
		$orderDAO = new Order();

		
		if ($this->getRequest()->isPost())
		{
			
			$dateFrom = $this->_getParam('from');
			$dateTo   = $this->_getParam('till');
			$roomType = $this->_getParam('type');
			$bookDAO = new Book();
			
			if ($this->_getParam('act') == "approve")
			{
				$bookDAO->setBlockInterval($dateFrom, $dateTo, $roomType);
				$orderDAO->approve($id);
			}
			else 
			{
				$orderDAO->cancel($id);	
			}	
			
			$this->_redirect('book/admin');
		}
		
				
		$order = $orderDAO->get($id);
		$this->view->Order = $order;
		
	}
	
	
	
	function manualAction()
	{
	    
	    $this->view->layout()->setLayout('admin');	
	    
	if ($this->getRequest()->isPost())
		{
			$dateFrom = $this->_getParam('from');
			$dateTo   = $this->_getParam('till');
			$roomType = $this->_getParam('type');
			$bookDAO = new Book();
			if ( ! $bookDAO->checkInterval($dateFrom, $dateTo, $roomType) )    
			{
			    $this->view->Message = "Интревал дат уже занят для этого номера";
			    $this->view->data = $this->_getAllParams();
			    return;
			}
			
			
			
			$orderDAO = new Order();
			$id = $orderDAO->create($dateFrom, $dateTo, $roomType, $this->_getAllParams());
			$orderDAO->approve($id);
			$bookDAO->setBlockInterval($dateFrom, $dateTo, $roomType);
			
			
			$this->_redirect('book/admin');
		}
	}
}

